package jaux.tank.generator.jd.components;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class JdCheckBoxGroup<T>  extends JdLabeledInput<List<T>, List<T>>{

	private static final long serialVersionUID = 1L;
	JPanel panel ;
	Map<T, JCheckBox> checkMap;

	public JdCheckBoxGroup(String label, List<T> items) {
		this(label, null, items);
	}
	
	public JdCheckBoxGroup(String label, List<T> defaultValue, List<T> params) {
		super(label, defaultValue, params);
		panel.setPreferredSize(null);
		
	}

	@Override
	public void registerChangeHandler(Consumer<List<T>> handler) {
		checkMap.forEach((k,v)->v.addItemListener(e->handler.accept(getValue())));
	}

	@Override
	public JComponent createInputComponent(List<T> params) {
		panel = new JPanel();
		panel.setLayout(new FlowLayout(FlowLayout.LEFT, 10,5));
		checkMap =  new HashMap<>();
		for(T item: params) {
			JPanel itemPanel = new JPanel();
			itemPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 1,1));
			panel.add(itemPanel);
			
			JLabel itemLabel = new JLabel(item.toString());
			itemPanel.add(itemLabel);
			JCheckBox itemCheckBox = new JCheckBox();
			itemLabel.setLabelFor(itemCheckBox);
			checkMap.put(item, itemCheckBox);
			itemPanel.add(itemCheckBox);
		}
		return panel;
	}

	@Override
	public void setEditable(boolean b) {
		checkMap.values().forEach(v->v.setEnabled(b));
	}

	@Override
	public List<T> getValue() {
		List<T> result = new ArrayList<>();
		for(Map.Entry<T, JCheckBox> entry: checkMap.entrySet()) {
			if(entry.getValue().isSelected()) {
				result.add(entry.getKey());
			}
		}
		return result;
	}

	@Override
	public void setValue(List<T> value) {
		for(Map.Entry<T, JCheckBox> entry: checkMap.entrySet()) {
			entry.getValue().setSelected(value !=null &&value.contains(entry.getKey()));
		}
	}
	

	public static void main(String[] args) {
		JFrame frame =  new JFrame();
		frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		frame.setLayout(new GridLayout(0, 1));
		JdCheckBoxGroup<String> jdCheckBoxGroup = new JdCheckBoxGroup<>("test", Arrays.asList("aaa", "bbbb", "cccc","444","555"));
		frame.getContentPane().add(jdCheckBoxGroup);
		JButton getValueButton = new JButton();
		getValueButton.setText("get value");
		getValueButton.addActionListener(e -> {
			JOptionPane.showMessageDialog(frame, jdCheckBoxGroup.getValue());
		});
		frame.getContentPane().add(getValueButton);
		// set value button
		JButton setValueButton = new JButton();
		setValueButton.setText("set value");
		setValueButton.addActionListener(e -> {
			jdCheckBoxGroup.setValue(Arrays.asList("aaa", "bbbb", "cccc"));
		});
		frame.getContentPane().add(setValueButton);
		frame.pack();
		frame.setVisible(true);
	}

}
